package com.zx.web.service.impl;

import com.zx.bean.Order;
import com.zx.bean.User;
import com.zx.web.dao.AdminOrderDao;
import com.zx.web.dao.impl.AdminOrderDaoImpl;
import com.zx.web.service.AdminOrderService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.service.impl
 * @Description:
 * @Date 2022/9/16 16:12
 */
public class AdminOrderServiceImpl implements AdminOrderService {

    AdminOrderDao adminOrderDao = new AdminOrderDaoImpl();

    @Override
    public List<Order> getAllOrder() {
        List<Order> orderList = adminOrderDao.getAllOrder();

        if (orderList!=null) {
            for (Order order : orderList) {
                User user = adminOrderDao.findUserById(order.getUid());
                order.setUsername(user.getUsername());
            }
        }
        return orderList;
    }

    @Override
    public void updateStatus(String oid) {
        adminOrderDao.updateStatus(oid);
    }

    @Override
    public List<Order> searchOrder(String username, String status) {
        List<Order> list = new ArrayList<>();
        List<Order> list1 = new ArrayList<>();
        List<Order> orderList = adminOrderDao.getAllOrder();


        if (orderList!=null) {
            for (Order order : orderList) {
                User user = adminOrderDao.findUserById(order.getUid());
                order.setUsername(user.getUsername());
            }
        }

        if(username!=null){
            for (Order order : orderList) {
                if (username.equalsIgnoreCase(order.getUsername())) {
                    list.add(order);
                }
            }
            if (status!=null) {
                for (Order order : list) {
                    if (order.getStatus().equalsIgnoreCase(status)) {
                        list1.add(order);
                    }
                }
                return list1;
            }else{
                System.out.println(""+list);
                return list;
            }

        }else{
            if(status!=null){
                for (Order order : orderList) {
                    if(status.equalsIgnoreCase(order.getStatus())){
                        list.add(order);
                    }
                }
                System.out.println("username=null&&status!=null"+list);
                return list;
            }else{
                return null;
            }

        }
    }
}
